import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BadugiTest {
    private Deck deck;
    private Deck deck1;
    private Hand hand;
    private Hand hand1;
    private Hand hand2;
    private Hand hand3;
    private Hand hand4;
    private Hand hand5;
    private Hand hand6;
    private Hand hand7;
    @Before
    public final void setUp() throws Exception {
        deck = new Deck();
        deck1 = new Deck();
        hand = new Hand();
        hand1 = new Hand();
        hand2 = new Hand();
        hand3 = new Hand();
        hand4 = new Hand();
        hand5 = new Hand();
        hand6 = new Hand();
        hand7 = new Hand();
        hand1.cardAdd(7, "D");
        hand1.cardAdd(8, "D");
        hand1.cardAdd(9, "C");
        hand1.cardAdd(7,"S");
        hand2.cardAdd(1, "S");
        hand2.cardAdd(5, "C");
        hand2.cardAdd(8, "D");
        hand2.cardAdd(10, "H");
        hand3.cardAdd(9, "H");
        hand3.cardAdd(11, "H");
        hand3.cardAdd(9, "D");
        hand3.cardAdd(11, "D");
        hand4.cardAdd(7,"D");
        hand4.cardAdd(7,"C");
        hand4.cardAdd(7,"H");
        hand4.cardAdd(7,"S");
        hand5.cardAdd(5,"S");
        hand5.cardAdd(8,"S");
        hand5.cardAdd(10,"S");
        hand5.cardAdd(11,"S");
        hand6.cardAdd(5,"C");
        hand6.cardAdd(8,"C");
        hand6.cardAdd(10,"C");
        hand6.cardAdd(11,"C");
    }

    @After
    public final void tearDown() throws Exception {
        deck = null;
        deck1 = null;
        hand = null;
        hand1 = null;
        hand2 = null;
        hand3 = null;
        hand4 = null;
        hand5 = null;
        hand6 = null;
        hand7 = null;
    }

    @Test(timeout = 10)
    public final void testShufflingTime() {
        deck.shuffle();
    }
    
    @Test
    public final void testShuffle() {
    	Card card = deck1.showCard(0);
    	deck1.shuffle();
    	Card card1 = deck1.showCard(0);
    	assertTrue("Same card, shuffling might be wrong", card.getRank() !=  card1.getRank() || card.getSuit() != card1.getSuit());
    	
    }
    
    @Test
    public final void testFindCard() {
    	assertTrue("Card not found", deck.findCard(5, "C"));
    }
    
    @Test
    public final void testCheckDeck() {
    	boolean result = true;
    	for (int i = 0; i < 13; i++) {
    		if(!deck.findCard(i, "C") || !deck.findCard(i, "H") || !deck.findCard(i, "D") || !deck.findCard(i, "S"))
    			result = false;
    	}
    	assertTrue("Deck is incomplete!", result);
    }
    
    @Test(expected = BadugiException.class)
    public final void wrongValueTest() throws BadugiException {
           Card card = new Card(14, "C");
    }
    
    @Test(expected = BadugiException.class)
    public final void wrongSuitTest() throws BadugiException {
           Card card = new Card(10, "X");
    }
    
    @Test(expected = BadugiException.class)
    public final void tableException() throws BadugiException {
        Table table = new Table(3, 4, 100, 20, 20);
    }
    
    @Test
    public final void testDrawCard() {
        hand.cardPick(deck);
        int rank = 0;
        String suit = "";
        try {
            suit = hand.getCard(0).getSuit();
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            rank = hand.getCard(0).getRank();
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        assertFalse("Card illegaly found!", deck.findCard(rank, suit));
    }
    
    @Test
    public final void testCardAdd() {
    	hand7.cardAdd(3,"H");
    	assertTrue("Card not found!", hand7.findCard(3, "H"));
    }
    
    @Test
    public final void test4Evaluation() {
        try {
            assertTrue("Wrong evaluation!", hand2.getHandRank() == 4);
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public final void test3Evaluation() {
        try {
            assertTrue("Wrong evaluation!", hand1.getHandRank() == 3);
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public final void test2Evaluation() {
        try {
            assertTrue("Wrong evaluation!", hand3.getHandRank() == 2);
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public final void test1Evaluation() {

        try {
            assertTrue("Wrong evaluation!", hand4.getHandRank() == 1);
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public final void equalityTest() {

        assertTrue("Not equal!", hand6.compareTo(hand5) == 0);
    }
    
    @Test 
    public final void compareTest() {
        assertTrue("Wrong compare!", hand2.compareTo(hand3) > 0);
    }
    
}
